#!/bin/bash
#
#每台k8s节点都要执行

#定义ip地址
master01="192.168.0.56"
node01="192.168.0.58"
node02="192.168.0.59"
node03="192.168.0.60"

#定义安装版本
KUBERNETES_VERSION="1.23.1-00"

#获取ip地址
ip="`ifconfig ens33 |awk -F '[ :]+' 'NR==2 {print $3}'`"


#关闭防火墙
sudo ufw disable && sudo ufw status

#永久selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config 

#临时关闭selinux
setenforce 0

#永久关闭swap
sed -ri 's/.*swap.*/#&/' /etc/fstab

#临时关闭swap
swapoff -a 
free -m

#设置主机名
if [ $master01 = $ip ];then
	hostnamectl set-hostname master01
elif [ $node01 = $ip ];then
	hostnamectl set-hostname node01
elif [ $node02 = $ip ];then
	hostnamectl set-hostname node02
elif [ $node03 = $ip ];then
	hostnamectl set-hostname node03
else
	echo "ip not found"
fi 

#添加hosts
cat >> /etc/hosts << EOF
$master01 master01
$node01 node01
$node02 node02
$node03 node03
EOF

#时间同步
sudo apt install ntpdate
sudo ntpdate time.windows.com

#修改时区为CST
sudo cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
date

#安装ipvsadm
sudo apt install ntpdate

#设置系统参数允许路由转发，不对网桥的数据进行处理
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system


#部署kubelet、kubeadm、kubectl
apt-get update && apt-get install -y apt-transport-https 

#使用阿里云源安装kubeadm
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -  

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet=${KUBERNETES_VERSION} kubeadm=${KUBERNETES_VERSION} kubectl=${KUBERNETES_VERSION}
systemctl daemon-reload
systemctl restart kubelet
